/*
 * @Description: http 封装
 * @Author: LiuYan
 * @Date: 2021-12-05 13:54:34
 * @LastEditTime: 2024-02-29 15:37:00
 * @LastEditors: sueRimn
 * @Reference:
 */
import axios from "axios";
import store from "@/store";
import router from "@/router";
import { Message } from "element-ui";
const { HTTP, APP } = require("../config/index.js");

//截取地址中的参数
let getQueryString = (name, search) => {
  search =
    search ||
    window.location.search.substr(1) ||
    window.location.hash.split("?")[1];
  if (!search) return null;
  let reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
  let r = search.match(reg);
  if (r != null) return unescape(r[2]);
  return null;
};

if (getQueryString("zj_unicom_token")) {
  localStorage.setItem("hltToken", getQueryString("zj_unicom_token"));
} else {
  // Message({
  //   type: "warning",
  //   message: "参数有误",
  // });
}

if (window.location.origin.includes("https://guangde.judaiot.com:30110")) {
  axios.defaults.baseURL = "https://guangde.judaiot.com:30110/gd/";
}
if (window.location.origin.includes("https://xfl.judaiot.com:30110")) {
  axios.defaults.baseURL = "https://xfl.judaiot.com:30110/xfl/";
}

// /生产环境配置请求域名
// if (process.env.NODE_ENV === "production")
//   axios.defaults.baseURL = "https://test-guangde.judaiot.com/";
// axios.defaults.baseURL = "https://hengtang.judaiot.com:4443/pda/"; // 外网域名
//请求拦截
axios.interceptors.request.use(function(config) {
  if (config.isLjxd && config.url.includes("/auth/oauth/token")) {
    config.headers["Authorization"] = "Basic cGVya2luOjEyMzQ1Ng==";
  }
  if (
    config.isLjxd &&
    localStorage.getItem("ljxdToken") &&
    !config.url.includes("/auth/oauth/token")
  ) {
    config.headers["Authorization"] = `Bearer ${localStorage.getItem(
      "ljxdToken"
    )}`;
  }
  if (!config.isLjxd && localStorage.getItem("Token")) {
    config.headers["BearerToken"] = `${localStorage.getItem("Token")}`;
  }
  return config;
});
//响应拦截
axios.interceptors.response.use((res) => {
  if (res.data.code != 200) {
    Message({
      type: "warning",
      message: res.data.msg,
    });
  }
  const _res = res.data;
  // if (_res.code === 401 && APP.isLogin) {
  //   router.replace('/login')
  // }
  if (_res.code === 500) {
    // console.error('500')
  }
  return _res;
});
const fetch = ({
  url,
  method = "GET",
  params,
  data = {},
  headers = {
    "content-type": "application/json",
  },
  token = true,
  ...other
}) => {
  const options = {
    url,
    method,
    data,
    params,
    headers,
    ...other,
  };
  if (token) {
    // console.log("=============", localStorage.getItem("userStore"));
    // console.log("httpToken:>>>>", localStorage.getItem("hltToken"));
    options.headers["token"] = localStorage.getItem("hltToken");
    // console.log(getQueryString("zj_unicom_token"), ">>>>>>>>>>>>");
    // options.headers["token"] =
    //   "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJleHBpcmVfdGltZSI6MTcwNjMxODEyMjEyOSwidGVuYW50Q29kZSI6Inp3ajczenoiLCJ1c2VySWQiOjI5OH0.ZJsucvpcmbC950n2DUOhainjX2T24anV6kkr3vMIXsE";
  }

  return new Promise((resolve, reject) => {
    axios(options)
      .then((res) => {
        resolve(res);
      })
      .catch((err) => {
        const res = err.response;

        // if (res.status === 500) {
        //   console.error("500");
        // }
        reject(err);
      });
  });
};
export default fetch;
